<?php

namespace App\Services;

use App\Contract\RedisKey;
use App\Exceptions\GeneralException;
use App\Models\Admin;
use App\Models\Merchant;
use App\Models\Privilege;
use App\Models\User;
use App\Tools\Tree;
use Illuminate\Support\Facades\Redis;

class PrivilegeService
{
    /**
     * 根据用户获取权限菜单
     * @param Admin $admin
     * @return array|mixed
     */
    public function getMenusByAdmin(Admin $admin)
    {
        $redisKey = RedisKey::ADMIN_MENUS;
        if ($privileges = Redis::hget($redisKey, $admin->id)) {
            return json_decode($privileges);
        } else {
            $privileges = [];
            if ($admin->id == 1) {
                $list = Privilege::query()->where(['type'=>'menu','platform'=>'admin'])->orderByDesc('sort')->get();
                foreach ($list as $privilege) {
                    $privileges[$privilege->code] = $privilege;
                }
            } else {
                $roles = $admin->roles;
                foreach ($roles as $role) {
                    foreach ($role->privileges->sortByDesc('sort') as $privilege) {
                        if($privilege->type == 'menu'){
                            $privileges[$privilege->code] = $privilege;
                        }
                    }
                }
            }
            $menus = Tree::getTree($privileges);
            Redis::hset($redisKey, $admin->id, json_encode($menus));
            Redis::expire($redisKey, 86400);
            return $menus;
        }
    }

    /**
     * 全部权限
     * @return array
     */
    public function allPrivilege()
    {
        $list = Privilege::query()->where(['platform'=>'admin'])->get()->toArray();
        return Tree::getTree($list,'base','code','parent_code');
    }

    public function getMenuByMerchant(Merchant $merchant)
    {
        $redisKey = RedisKey::USER_MENUS;
        if ($privileges = Redis::hget($redisKey, $merchant->id)) {
            return json_decode($privileges);
        } else {
            $privileges = [];
            $list = Privilege::query()
                ->where(['type'=>'menu','platform'=>'user'])
                ->whereRaw("FIND_IN_SET('$merchant->type',`user_type`)")
                ->orderByDesc('sort')->get();
            foreach ($list as $privilege) {
                $privileges[$privilege->code] = $privilege;
            }
            $menus = Tree::getTree($privileges);
            Redis::hset($redisKey, $merchant->id, json_encode($menus));
            Redis::expire($redisKey, 86400);
            return $menus;
        }
    }

    /**
     * 检查后台用户有没有操作权限
     * @param Admin $admin
     * @param string $privilegeCode
     * @return bool
     * @throws GeneralException
     */
    public function checkAdminPrivileges(Admin $admin,string $privilegeCode)
    {
        if($admin->id == 1){
            return true;
        }
        $roles = $admin->roles;
        $res = null;
        foreach ($roles as $role){
            $res = $role->privileges->firstWhere('code',$privilegeCode);

        }
        if(empty($res)){
            throw new GeneralException('您没有操作权限');
        }
        return true;
    }

    public function getMenusByUser(User $user)
    {
        $redisKey = RedisKey::APP_USER_MENU;
        if ($privileges = Redis::hget($redisKey, $user->id)) {
            return json_decode($privileges);
        } else {
            $privileges = [];
            $list = Privilege::query()
                ->where(['type'=>'menu','platform'=>'app_user'])
                ->orderByDesc('sort')->get();
            foreach ($list as $privilege) {
                $privileges[$privilege->code] = $privilege;
            }
            $menus = Tree::getTree($privileges);
            Redis::hset($redisKey, $user->id, json_encode($menus));
            Redis::expire($redisKey, 86400);
            return $menus;
        }
    }


}
